/**
 * 
 */
package edu.ucmo.cis.enrollment.dao;

import java.util.List;
import javax.jdo.Extent;
import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import edu.ucmo.cis.enrollment.vo.SystemAdmin;
import org.springframework.orm.jdo.support.JdoDaoSupport;

/**
 * @author Jiang
 * Email:sjiangmo@gmail.com
 */
public class SystemAdminJdoDao extends JdoDaoSupport implements SystemAdminDao {

	/* (non-Javadoc)
	 * @see edu.ucmo.cis.enrollment.dao.SystemAdminDao#insertSystemAdmin(java.lang.String, java.lang.String)
	 */
	@Override
	public void insertSystemAdmin(String loginName, String password) {
		 PersistenceManager pm = getPersistenceManager();
	        SystemAdmin systemAdmin = new SystemAdmin("cisadmin", "123456");
	        try {
	            pm.makePersistent(systemAdmin);
	        } finally {
	            pm.close();
	        }
	}

	/* (non-Javadoc)
	 * @see edu.ucmo.cis.enrollment.dao.SystemAdminDao#checkLogin(java.lang.String, java.lang.String)
	 */
	@Override
	public SystemAdmin searchByLoginName(String loginName) {
		PersistenceManager pm = getPersistenceManager();
		SystemAdmin systemAdmin=null;
		try {
			Extent r = pm.getExtent (SystemAdmin.class, false);
			String filter = "loginName == "+loginName;
			Query query = pm.newQuery(r,filter);
			List<SystemAdmin> list = (List<SystemAdmin>) query.execute();
			if(!list.isEmpty())
				systemAdmin = list.get(0);
			return systemAdmin;
		}catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/* (non-Javadoc)
	 * @see edu.ucmo.cis.enrollment.dao.SystemAdminDao#changePassword(edu.ucmo.cis.enrollment.vo.SystemAdmin, java.lang.String)
	 */
	@Override
	public void changePassword(SystemAdmin systemAdmin, String password) {
		PersistenceManager pm = getPersistenceManager();
	    try {
	        SystemAdmin s = pm.getObjectById(SystemAdmin.class, systemAdmin.getId());
	        s.setPassword(password);
	    }catch (Exception e) {
			e.printStackTrace();
	    } finally {
	        pm.close();
	    }

	}

	/* (non-Javadoc)
	 * @see edu.ucmo.cis.enrollment.dao.SystemAdminDao#updateEmail(edu.ucmo.cis.enrollment.vo.SystemAdmin, java.lang.String)
	 */
	@Override
	public void updateEmail(SystemAdmin systemAdmin, String email) {
		PersistenceManager pm = getPersistenceManager();
	    try {
	        SystemAdmin s = pm.getObjectById(SystemAdmin.class, systemAdmin.getId());
	        s.setEmail(email);
	    }catch (Exception e) {
			e.printStackTrace();
	    } finally {
	        pm.close();
	    }

	}

}
